//
//  CLUIbutton.m
//  greatControl
//
//  Created by Phyllis Hu on 15/10/22.
//  Copyright (c) 2015年 dingyi. All rights reserved.
//

#import "CLUIbutton.h"
#import "NSString+composed.h"

#define TitleW  50
#define DefaultImageW 12
#define Space  10

@interface  CLUIbutton()


@end

@implementation CLUIbutton

/**
 * imageW -> 按钮图片的宽度
 * titleW -> 按钮文字的宽度
 * hight  -> 控件整体的高度
 */

- (id)initImageW:(CGFloat)imageW titleW:(CGFloat)titleW hight:(CGFloat)hight{
    
    _BtnTitleW = titleW;
    _BtnImageW = imageW;
    //计算控件的frame
    CGRect rect = CGRectMake(0, 0,(_BtnTitleW+_BtnImageW+2*Space), hight);
    
    self = [super initWithFrame:rect];
    if (self) {
        
        //设置图标居中显示
        self.imageView.contentMode = UIViewContentModeCenter;
        
        //设置文字右对齐
        self.titleLabel.textAlignment = NSTextAlignmentRight;
        
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        //        self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;  //多行文字显示
        self.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;  //"abcd..."
        //设置高亮时候不要调整图标为灰色
        self.adjustsImageWhenHighlighted = NO;
    }
    return self;
}

/**
 *     设置图标frame
 */
-(CGRect)imageRectForContentRect:(CGRect)contentRect
{

    CGFloat imageX = 0;
    if(_BtnTitleW){
        imageX = _BtnTitleW;
    }else{
        imageX =TitleW;
    }
    imageX += Space;
    CGFloat imageY = 0;
    CGFloat imageW = 0;
    if(self.BtnImageW){
        imageW = self.BtnImageW;
    }else{
        imageW = DefaultImageW;
    }
    
    CGFloat imageH = self.frame.size.height;
    return CGRectMake(imageX, imageY, imageW, imageH);
}

/**
 *     设置内部文字的frame
 */
-(CGRect)titleRectForContentRect:(CGRect)contentRect
{  
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = 0;
    
    if(_BtnTitleW){
        titleW = _BtnTitleW;
    }else{
        titleW =TitleW;
    }
    CGFloat titleH = self.frame.size.height;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

/**
 *   重新布局按钮的内部结构
 */
-(void) againLayoutBtnImageW:(CGFloat)BtnImageW BtnTitleW:(CGFloat)BtnTitleW{

    _BtnTitleW = BtnTitleW;
    _BtnImageW = BtnImageW;

    self.frame = CGRectMake(0, 0,(_BtnTitleW+_BtnImageW+2*Space), self.frame.size.height);
}

- (void)setBtnTitleW:(float)BtnTitleW{
    _BtnTitleW = BtnTitleW;
    [self setNeedsLayout];
}


- (void)setBtnImageW:(float)BtnImageW{
    _BtnImageW = BtnImageW;
    [self setNeedsLayout];
}



@end
